﻿using CommandLib;
using Microsoft.AspNetCore.Mvc;
using NPOI.SS.Formula.Functions;
using SignalrChat.Buesses;
using SignalrChat.Models;

namespace SignalrChat.Controllers
{
    /// <summary>
    /// 用户服务
    /// </summary>
    public class UsersController : BaseController
    {
        private UsersBll usersBll;
        public UsersController(IHttpContextAccessor httpContext) : base(httpContext)
        {
            CookiesHelper.Init(httpContext);
            usersBll = new UsersBll();
        }
        public IActionResult Index()
        {
            return View();
        }

        public IActionResult login()
        {
            return View();
        }

        public IActionResult register()
        {

            return View();

        }
        /// <summary>
        /// 用户登陆
        /// </summary>
        /// <param name="account"></param>
        /// <param name="pwd"></param>
        /// <returns></returns>
        public RtModel<object> userlogin(string account, string pwd)
        {

            var result = new RtModel<object>();

            var ec = MemoryCacheHelper.GetData(CookiesKey.userErrCount + account?.Trim());
            if (ec != null)
            {
                var errcont = Convert.ToInt32(ec);
                if (errcont >= 5)
                {
                    result.code = 300;
                    result.data = false;
                    result.message = "登陆异常超过5次！";
                    return result;
                }
            }
            var users = usersBll.GetUsers();
            if (users != null)
            {
                var Account = users.Where(w => w.Account == account?.Trim()).FirstOrDefault();
                if (Account != null)
                {

                    if (Account.PassWord == Md5Helper.MD5Encrypt(pwd?.Trim()))
                    {
                        result.code = 200;
                        result.data = true;
                        var userlogin = DesHelper.DESEncrypt(Account.ToJsonString());
                        CookiesHelper.SetCookies(CookiesKey.userLoginKey, userlogin,60*12);
                        MemoryCacheHelper.Remove(CookiesKey.userErrCount + account?.Trim());
                        usersBll.UpdateLoginTime(Account.Id, DateTime.Now);
                    }
                    else
                    {
                        int errcount = Convert.ToInt32(ec);
                        result.code = 300;
                        result.message = "密码错误!";
                        result.data = false;
                        MemoryCacheHelper.Add(CookiesKey.userErrCount + account?.Trim(), ++errcount, TimeSpan.FromMinutes(5));
                    }

                }
                else
                {
                    result.code = 0;
                    result.message = "用户不存在！";
                }
            }

            return result;




        }
        /// <summary>
        /// 用户注册
        /// </summary>
        /// <param name="users"></param>
        /// <returns></returns>
        public RtModel<bool> userRegister(Users users)
        {
            var result = new RtModel<bool>();
            if (users.Account.IsEmpty())
            {
                result.code = 300;
                result.data = false;
                result.message = "帐号不能为空！";
                return result;
            }
            if (users.PassWord.IsEmpty()) {

                result.code = 300;
                result.data = false;
                result.message = "密码不能为空！";
                return result;
            }
            if (users.NickName.IsEmpty())
            {
                result.code = 300;
                result.data = false;
                result.message = "昵称不能为空！";
                return result;
            }

            var userlist = usersBll.GetUsers();
            if (userlist != null)
            {
                var hasaccount = userlist.Where(w => w.Account == users.Account).Any();
                if (hasaccount)
                {
                    result.code = 300;
                    result.data = false;
                    result.message = "用户名以存在！";
                    return result;
                }
            }
            users.Account = users.Account.Trim();
            users.NickName = users.NickName.Trim();
            users.PassWord = Md5Helper.MD5Encrypt(users.PassWord);
            result.data = usersBll.AddUsers(users);

            return result;

        }

        /// <summary>
        /// 获取用户列表
        /// </summary>
        /// <returns></returns>
        public RtModel<object> getUserList()
        {
            var result = new RtModel<object>();

            result.data = ChatHub.GetUsers;

            return result;
        }
        /// <summary>
        /// 退出
        /// </summary>
        /// <returns></returns>
        public RtModel<object> exit()
        {
            CookiesHelper.DeleteCookies(CookiesKey.userLoginKey);
            MemoryCacheHelper.Remove(CookiesKey.userErrCount + this.users?.Account.Trim());           
            return new RtModel<object>();
        }
    }
}
